// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_HR_MONTH_SALARY
// * CreateDate    : 2011/10/27 13:18:50
// * Creater       : CodeSmith
// * LastChangeDate: 2011/10/27 13:18:50
// * LastChanger   : 2011/10/27 13:18:50
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;
using ESOL.Common;

namespace ESOL.Business.Hr
{
/// <summary>
    /// HrMonthSalary
    /// </summary>
    public partial class HrMonthSalaryBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objHrMonthSalaryEntity">Entity</param>
        /// <returns>Entity</returns>
        public static HrMonthSalaryEntity GetHrMonthSalary(HrMonthSalaryEntity objHrMonthSalaryEntity)
        {
            return DataAccess.SelectSingle<HrMonthSalaryEntity>(objHrMonthSalaryEntity);
        }
        public static DataTable GetByCondition(string condition, string yearMonth, string orderBy)
        {
            string sql = @"SELECT P.PERSON_ID AS SELECTED_PERSON, '" + yearMonth + @"' AS SALARY_DATE, P.PERSON_CODE, P.NAME_CN,S.NAME_CN AS SUPERVISOR_NAME,
                            D.DEPARTMENT_NAME,(SELECT MP.POSITION_NAME + ';'
                                FROM MST_POSITION MP
                                INNER JOIN HR_PERSON_POSITION PP ON PP.POSITION_ID = MP.POSITION_ID
                                WHERE PP.PERSON_ID = P.PERSON_ID GROUP BY MP.POSITION_NAME FOR XML PATH('')) AS POSITION_NAME,
                            MS.*,CONVERT(DECIMAL(18,2),TOTAL_SALARY) AS SALARY,
                            CASE WHEN MS.STATUS IS NULL THEN 'MSS01' ELSE MS.STATUS END AS STATUS_NAME, P.STATUS AS PERSON_STATUS, CONVERT(NVARCHAR(10),E.JOIN_DATE,120) AS JOIN_DATE
                            FROM MST_PERSON P
                            LEFT JOIN HR_EMPLOYEE E ON E.PERSON_ID = P.PERSON_ID
                            LEFT JOIN HR_MONTH_SALARY MS ON MS.PERSON_ID = P.PERSON_ID AND MS.SALARY_MONTH = '" + yearMonth + @"'
                            LEFT JOIN MST_DEPARTMENT D ON D.DEPARTMENT_ID = P.DEPARTMENT_ID
                            LEFT JOIN MST_PERSON S ON S.PERSON_ID = P.SUPERVISOR
                            WHERE 1=1 ";
            if (string.IsNullOrEmpty(condition) == false)
            {
                sql += condition;
            }
            if (string.IsNullOrEmpty(orderBy) == false)
            {
                sql += " ORDER BY " + orderBy;
            }
            DataSet ds = DataAccess.SelectDataSet(sql);
            if (ds != null && ds.Tables.Count > 0)
            {
                return ds.Tables[0];
            }
            return null;
        }
        public static DataTable GetMySalaryByCondition(string condition, string orderBy, string rowCount)
        {
            string sql = CommonFunctions.GetSelect(rowCount) + @"P.PERSON_ID AS SELECTED_PERSON, MS.SALARY_MONTH AS SALARY_DATE, P.PERSON_CODE, P.NAME_CN,
                            D.DEPARTMENT_NAME,(SELECT MP.POSITION_NAME + ';'
                                FROM MST_POSITION MP
                                INNER JOIN HR_PERSON_POSITION PP ON PP.POSITION_ID = MP.POSITION_ID
                                WHERE PP.PERSON_ID = P.PERSON_ID GROUP BY MP.POSITION_NAME FOR XML PATH('')) AS POSITION_NAME,
                            MS.*,CONVERT(DECIMAL(18,2),TOTAL_SALARY) AS SALARY,
                            CASE WHEN MS.STATUS IS NULL THEN 'MSS01' ELSE MS.STATUS END AS STATUS_NAME, P.STATUS AS PERSON_STATUS
                            FROM HR_MONTH_SALARY MS
							INNER JOIN MST_PERSON P ON MS.PERSON_ID = P.PERSON_ID
                            LEFT JOIN HR_EMPLOYEE E ON E.PERSON_ID = P.PERSON_ID
                            LEFT JOIN MST_DEPARTMENT D ON D.DEPARTMENT_ID = P.DEPARTMENT_ID
                            WHERE 1=1 ";
            if (string.IsNullOrEmpty(condition) == false)
            {
                sql += condition;
            }
            if (string.IsNullOrEmpty(orderBy) == false)
            {
                sql += " ORDER BY " + orderBy;
            }
            DataSet ds = DataAccess.SelectDataSet(sql);
            if (ds != null && ds.Tables.Count > 0)
            {
                return ds.Tables[0];
            }
            return null;
        }
        #endregion

        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objHrMonthSalaryEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertHrMonthSalary(HrMonthSalaryEntity objHrMonthSalaryEntity)
        {
            return DataAccess.Insert(objHrMonthSalaryEntity);
        }

        public static bool InsertHrMonthSalary(List<HrMonthSalaryEntity> objHrMonthSalaryEntityList)
        {
            return DataAccess.Insert(objHrMonthSalaryEntityList);
        }
        #endregion

        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objHrMonthSalaryEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateHrMonthSalary(HrMonthSalaryEntity objHrMonthSalaryEntity)
        {
            return DataAccess.Update(objHrMonthSalaryEntity);
        }

        public static bool UpdateHrMonthSalary(List<HrMonthSalaryEntity> objHrMonthSalaryEntityList)
        {
            return DataAccess.Update<HrMonthSalaryEntity>(objHrMonthSalaryEntityList);
        }
        #endregion

        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objHrMonthSalaryEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteHrMonthSalary(HrMonthSalaryEntity objHrMonthSalaryEntity)
        {
            return DataAccess.Delete(objHrMonthSalaryEntity);
        }

        public static bool DeleteHrMonthSalary(List<HrMonthSalaryEntity> objHrMonthSalaryEntityList)
        {
            return DataAccess.Delete<HrMonthSalaryEntity>(objHrMonthSalaryEntityList);
        }
        #endregion

        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetHrMonthSalaryAll()
        {
            return DataAccess.SelectAllDataSet<HrMonthSalaryEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<HrMonthSalaryEntity> GetHrMonthSalaryAllList()
        {
            return DataAccess.SelectAll<HrMonthSalaryEntity>();
        }
        #endregion

        #region Functions
        public static bool SaveMonthSalary(bool isNew, HrMonthSalaryEntity monthSalary, DataTable dtDetail, List<HrMonthSalaryItemEntity> deleteList)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    if (isNew)
                    {
                        DataAccess.Insert(monthSalary, broker);
                    }
                    else
                    {
                        DataAccess.Update(monthSalary, broker);
                    }

                    //Save items
                    List<HrMonthSalaryItemEntity> insertList = new List<HrMonthSalaryItemEntity>();
                    List<HrMonthSalaryItemEntity> modifyList = new List<HrMonthSalaryItemEntity>();
                    foreach (DataRow dr in dtDetail.Rows)
                    {
                        HrMonthSalaryItemEntity entity = null;
                        switch (dr.RowState)
                        {
                            case DataRowState.Added:
                                entity = new HrMonthSalaryItemEntity(dr);
                                insertList.Add(entity);
                                break;
                            case DataRowState.Modified:
                                entity = new HrMonthSalaryItemEntity(dr);
                                modifyList.Add(entity);
                                break;
                            default:
                                continue;
                        }
                    }
                    DataAccess.Delete<HrMonthSalaryItemEntity>(deleteList, broker);
                    DataAccess.Update<HrMonthSalaryItemEntity>(modifyList, broker);
                    DataAccess.Insert<HrMonthSalaryItemEntity>(insertList, broker);

                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }
        #endregion
    }
	
	
}	

